<template>
  <!-- 新增员工弹层组件 -->
  <el-dialog title="新增员工" :visible="showDialog" @close="btnCancel">
    <!-- 绑定员工数据 -->
    <el-form
      ref="addEmployee"
      :model="formData"
      :rules="rules"
      label-width="120px"
    >
      <el-form-item prop="username" label="姓名">
        <el-input
          v-model="formData.username"
          style="width: 50%"
          placeholder="请输入姓名"
        />
      </el-form-item>

      <el-form-item prop="mobile" label="手机">
        <el-input
          v-model="formData.mobile"
          style="width: 50%"
          placeholder="请输入手机号"
        />
      </el-form-item>

      <el-form-item prop="timeOfEntry" label="入职时间">
        <el-date-picker
          v-model="formData.timeOfEntry"
          style="width: 50%"
          placeholder="请选择入职时间"
        />
      </el-form-item>

      <el-form-item prop="formOfEmployment" label="聘用形式">
        <el-select
          v-model="formData.formOfEmployment"
          style="width: 50%"
          placeholder="请选择"
        >
          <el-option
            v-for="item in EmployeeEnum.hireType"
            :key="item.id"
            :label="item.value"
            :value="item.id"
          />
        </el-select>
      </el-form-item>

      <el-form-item prop="workNumber" label="工号">
        <el-input
          v-model="formData.workNumber"
          style="width: 50%"
          placeholder="请输入工号"
        />
      </el-form-item>

      <el-form-item prop="departmentName" label="部门">
        <!-- @focus="getDepartments" 表单的获取焦点事件 -->
        <el-input
          v-model="formData.departmentName"
          style="width: 50%"
          placeholder="请选择部门"
          @focus="getDepartments"
        />
        <!-- 
          放置树形组件
          :data="treeData"  :props="{ label: 'name' }"  显示数据
          v-loading="loading" 显示进度条
          :default-expand-all="true"  默认展开树型
          node-click 节点被点击时的回调
         -->
        <el-tree
          v-if="showTree"
          v-loading="loading"
          :data="treeData"
          :props="{ label: 'name' }"
          :default-expand-all="true"
          @node-click="selectNode"
        />
      </el-form-item>

      <el-form-item prop="correctionTime" label="转正时间">
        <el-date-picker
          v-model="formData.correctionTime"
          style="width: 50%"
          placeholder="请选择转正时间"
        />
      </el-form-item>
    </el-form>
    <!-- footer插槽 -->
    <template v-slot:footer>
      <el-row type="flex" justify="center">
        <el-col :span="6">
          <!-- 点击取消关闭 -->
          <el-button size="small" @click="btnCancel">取消</el-button>
          <!-- 点击确定添加数据 -->
          <el-button type="primary" size="small" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </template>
  </el-dialog>
</template>

<script>
import { getDepartments } from "@/api/departments"; // 获取部门数据
import { transListToTreeData } from "@/utils"; // 导入 部门数据转化树形
import EmployeeEnum from "@/api/constant/employees";
import { addEmployee } from "@/api/employess"; // 新增员工接口
export default {
  props: {
    // 控制弹层的显示隐藏
    showDialog: {
      type: Boolean,
      default: false,
    },
  },
  data() {
    return {
      EmployeeEnum,
      formData: {
        username: "", // 姓名
        mobile: "13912345678", // 手机号
        formOfEmployment: "", // 聘用形式
        workNumber: "", // 工号
        departmentName: "", // 组织名称
        timeOfEntry: "", // 入职时间
        correctionTime: "", // 转正时间
      },
      // 校验规则
      rules: {
        username: [
          { required: true, message: "用户姓名不能为空", trigger: "blur" },
          {
            min: 1,
            max: 4,
            message: "用户姓名为1-4位",
            trigger: "blur",
          },
        ],
        mobile: [
          { required: true, message: "手机号不能为空", trigger: "blur" },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: "手机号格式不正确",
            trigger: "blur",
          },
        ],
        formOfEmployment: [
          { required: true, message: "聘用形式不能为空", trigger: "blur" },
        ],
        workNumber: [
          { required: true, message: "工号不能为空", trigger: "blur" },
        ],
        departmentName: [
          { required: true, message: "部门不能为空", trigger: "change" },
        ],
        timeOfEntry: [{ required: true, message: "入职时间", trigger: "blur" }],
      },
      treeData: [], // 存储树型数据
      loading: false, // 控制树的显示或隐藏进度条
      showTree: false, // 控制树型的显示或隐藏
    };
  },
  methods: {
    // 获取树型数据
    async getDepartments() {
      this.showTree = true; // 显示树型
      this.loading = true; // 显示进度条
      const { depts } = await getDepartments(); // 获取部门数据
      this.treeData = transListToTreeData(depts, ""); // 调用封装的 数据转树型 存储到 treeData
      this.loading = false; // 关闭进度条
    },
    // 点击部门赋值表单数据 node 当前点击的数据
    selectNode(node) {
      // console.log(node);
      this.formData.departmentName = node.name; // 将当前数据内的 name 赋值给 departmentName 实现数据回流
      this.showTree = false; // 关闭树型
    },
    // 点击确定 添加数据
    async btnOK() {
      try {
        await this.$refs.addEmployee.validate(); // 表单验证通过
        await addEmployee(this.formData); // 调用添加数据接口 传入数据
        // console.log(this.$parent);
        this.$parent.getEmployeeList(); // this.$parent 可以直接调用到父组件的实例 实际上就是父组件this  调用父组件的 getEmployeeList 方法重新拉取数据渲染页面
        this.$message.success("操作成功"); // 提示信息
        this.$parent.showDialog = false; // 关闭树型组件
        // 这里不用重置数据 因为 关闭弹层触发了 close 事件 close 事件绑定了 btnCancel 方法
      } catch (err) {
        console.log(err);
      }
    },
    // 点击关闭弹层
    btnCancel() {
      // 清空表单数据
      this.formData = {
        username: "",
        mobile: "",
        formOfEmployment: "",
        workNumber: "",
        departmentName: "",
        timeOfEntry: "",
        correctionTime: "",
      };
      this.$emit("update:showDialog", false); // 关闭弹层 update:showDialog 名称 这么写可以在父组件直接用 sync修饰符
      this.$refs.addEmployee.resetFields(); // 清空表单验证
    },
  },
};
</script>

<style></style>
